import reso.common.Network;
import reso.common.Node;
import reso.ip.IPAddress;
import reso.ip.IPHost;
import reso.ip.IPRouteEntry;
import reso.ip.IPRouter;
import reso.scheduler.AbstractScheduler;
import reso.scheduler.Scheduler;
import reso.utilities.NetworkBuilder;
import Protocol.DSRProtocol;


public class Main {

	/**
	 * @param args
	 */
	public static final String TOPO_FILE= "reso/data/topology.txt";
	
	public static void main(String[] args) {
	/* decouverte du chemin */
		String filename= Main.class.getClassLoader().getResource(TOPO_FILE).getFile();
		
		AbstractScheduler scheduler= new Scheduler();
		try {
			Network network= NetworkBuilder.loadTopology(filename, scheduler);
			// Add routing protocol application to each router
			for (Node n: network.getNodes()) {
				if (!(n instanceof IPRouter))
					continue;
				IPRouter router= (IPRouter) n;
				//boolean advertise= true;
				boolean advertise= (n.name.equals("R1") || n.name.equals("R4") );
//				boolean advertise=false;
				System.out.println(" router = " + router.name.toString() + ", advertise = " + advertise + ", loopback = "  + router.getIPLayer().getInterfaceByName("lo0").getAddress().toString());
				router.addApplication(new DSRProtocol(router, advertise, scheduler));
				router.start();
				
			}
			
			// Run simulation
			scheduler.run();
			
			// Display forwarding table for each node
			System.out.println("display FWDTable");
			for (Node n: network.getNodes()) {
				if (!(n instanceof IPRouter))
					continue;
				IPRouter router= (IPRouter) n;
				System.out.println("Router [" + router.name + "]");
				for (IPRouteEntry re: router.getIPLayer().getRoutes()) {
					if (re.dst.equals(IPAddress.getByAddress("10.0.0.4")))
						System.out.println("\t" + re);
				}
			}			
			
		} catch (Exception e) {
			System.err.println(e.getMessage());
			e.printStackTrace(System.err);
		}
		System.out.println("End");
		
	}

}
